//app.js
var QQMapWX = require('/libs/qqmap-wx-jssdk1.2/qqmap-wx-jssdk.js');
var qqmapsdk;
var route = require("/utils/route.js");
var network = require("/utils/network.js");
App({
    onLaunch: function () {
        // 展示本地存储能力
        var logs = wx.getStorageSync('logs') || []
        logs.unshift(Date.now())
        wx.setStorageSync('logs', logs);
        var that = this;
        wx.getSystemInfo({
            success(res) {
                var name = 'iPhone X'
                if (res.model.indexOf(name) > -1) {
                    that.globalData.isIpx = true;
                } else {
                    that.globalData.isIpx = false;
                }
            }
        })
        // wx.getLocation({
        //     type: 'gcj02', // 返回可以用于wx.openLocation的经纬度
        //     altitude: true,
        //     success(res) {
        //         const latitude = res.latitude
        //         const longitude = res.longitude
        //         console.log(latitude, longitude);
        //         // 实例化API核心类
        //         qqmapsdk = new QQMapWX({
        //             key: 'SB7BZ-SS43X-6VG4W-ZG2W3-VPBK6-6VFQD'
        //         });
        //         qqmapsdk.reverseGeocoder({//地址解析
        //             location: {
        //                 latitude: latitude,
        //                 longitude: longitude
        //             },
        //             success: function (res) {
        //                 console.log(res);
        //                 //获得地址
        //                 that.globalData['address'] = res.result.address;
        //             },
        //             fail: function (res) {
        //                 console.log(res);
        //             }
        //         });
        //     },
        //     fail: function (res) {
        //         console.log(res);
        //     }
        // })
        this.weChatLogin(); //登录
    },
    weChatLogin: function() {
        var that = this;
        // 登录
        wx.login({
            success: res => {
                console.log(res.code);
                // 发送 res.code 到后台换取 openId, sessionKey, unionId
                wx.request({
                    url: route.ROUTE_MINI_LOGIN,
                    data: { code: res.code, type: 0 },
                    header: { 'content-type': 'application/x-www-form-urlencoded;charset=utf-8', },
                    method: 'POST',
                    success: function (res) {
                        if (res.statusCode == 200) {
                            if (res.data.success) {
                                that.globalData['userID'] = res.data.context.userID;
                                that.globalData.userInfo = res.data.context;
                                console.log(that.globalData.userInfo);
                                if (that.plumSessionCallback) {
                                    that.plumSessionCallback(res.data.context.userID);
                                } else {
                                    that.globalData['userID'] = res.data.context.userID;
                                }
                            }
                        } else {
                            console.log(res.data);
                        }
                    },
                    fail: function (res) {
                        console.log(res);
                    },
                    complete: function (res) {

                    },
                })
            }
        })
    },
    globalData: {
        userInfo: null
    },
    route: route,
    network: network
})